package com.cebbank.sz.cebtemplate.listener;


import com.baomidou.mybatisplus.core.toolkit.AES;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
import org.springframework.cloud.consul.config.ConsulPropertySource;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource;

import java.util.HashMap;
import java.util.Iterator;

public class MyApplicationRunListener implements SpringApplicationRunListener {

    private final SpringApplication application;
    private final String[] args;

    public MyApplicationRunListener(SpringApplication sa, String[] args) {
        this.application = sa;
        this.args = args;
    }

    @Override
    public void starting(ConfigurableBootstrapContext bootstrapContext) {
        System.out.println("服务启动RunnerTest  SpringApplicationRunListener的starting方法...");
    }

    @Override
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
        System.out.println("服务启动RunnerTest  SpringApplicationRunListener的environmentPrepared方法...");
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("服务启动RunnerTest  SpringApplicationRunListener的contextPrepared方法...");
    }


    /*
    * 原来是针对OriginTrackedMapPropertySource从对应source中取出键值对进行比较以mpw:开头的就解密并放回envirment的PropertySources中
    * consul没法这么做需要最后针对enviroment进行处理
    *
    *
    * */

    private String getMPWKey(ConfigurableApplicationContext context)
    {
        String mpwKey = null;
        ConfigurableEnvironment environment=context.getEnvironment();
        Iterator var4 = environment.getPropertySources().iterator();

        while(var4.hasNext()) {
            PropertySource<?> ps = (PropertySource)var4.next();
            if (ps instanceof SimpleCommandLinePropertySource) {
                SimpleCommandLinePropertySource source = (SimpleCommandLinePropertySource)ps;
                mpwKey = source.getProperty("mpw.key");
                System.out.println("mpw.key:"+mpwKey);
                return mpwKey;
            }
        }
        return null;
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("服务启动RunnerTest  SpringApplicationRunListener的contextLoaded方法...");
        String mpwKey = this.getMPWKey(context);
        ConfigurableEnvironment environment=context.getEnvironment();
        if (StringUtils.isNotBlank(mpwKey)) {
            HashMap<String, Object> map = new HashMap();
            Iterator var15 = environment.getPropertySources().iterator();

            while(true) {
                PropertySource ps;
                do {
                    if (!var15.hasNext()) {
                        if (CollectionUtils.isNotEmpty(map)) {
                            environment.getPropertySources().addFirst(new MapPropertySource("custom-encrypt", map));
                            System.out.println("set custom-encrypt success!");
                        }

                        return;
                    }

                    ps = (PropertySource)var15.next();
                } while(!((ps instanceof OriginTrackedMapPropertySource) || (ps instanceof BootstrapPropertySource)));

                if (ps instanceof OriginTrackedMapPropertySource) {
                    OriginTrackedMapPropertySource source = (OriginTrackedMapPropertySource)ps;
                    String[] names = source.getPropertyNames();
                    int length = names.length;

                    for(int i = 0; i < length; ++i) {
                        String name = names[i];
                        Object value = source.getProperty(name);
                        if (value instanceof String) {
                            String str = (String)value;
                            if (str.startsWith("mpw:")) {
                                String valueDecrypt=AES.decrypt(str.substring(4), mpwKey);
                                map.put(name, valueDecrypt);
                                System.out.println("name:value is "+name+":"+valueDecrypt);
                            }
                        }
                    }
                }
                if (ps instanceof BootstrapPropertySource) {
                    BootstrapPropertySource source = (BootstrapPropertySource)ps;
                    String[] names = source.getPropertyNames();
                    int length = names.length;

                    for(int i = 0; i < length; ++i) {
                        String name = names[i];
                        Object value = source.getProperty(name);
                        if (value instanceof String) {
                            String str = (String)value;
                            if (str.startsWith("mpw:")) {
                                String valueDecrypt=AES.decrypt(str.substring(4), mpwKey);
                                map.put(name, valueDecrypt);
                                System.out.println("name:value is "+name+":"+valueDecrypt);
                            }
                        }
                    }

                }

            }
        }
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("服务启动RunnerTest  SpringApplicationRunListener的running方法...");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("服务启动RunnerTest  SpringApplicationRunListener的failed方法...");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("服务启动RunnerTest  SpringApplicationRunListener的started方法...");
    }
}

